﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-9-9
Description:OrderList三层结构数据访问层
=============================================================================
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SQLite;
using FTchina.Models;//引入模型层
namespace FTchina.DAL
{
	public static partial class OrderListService
	{
		#region 增加一条数据
		public static int AddOrderList(OrderList orderList)
		{
			string sql =
			"INSERT INTO OrderList(CustomerID,CType,OrderDate,OrderName,pcount) "+
			"VALUES (@CustomerID,@CType,@OrderDate,@OrderName,@pcount)";
			
			 
				SQLiteParameter[] para = new SQLiteParameter[]
				{
						
					new SQLiteParameter("@CustomerID",orderList.CustomerID),
						
					new SQLiteParameter("@CType",orderList.CType),
						
					new SQLiteParameter("@OrderDate",orderList.OrderDate),
						
					new SQLiteParameter("@OrderName",orderList.OrderName),
						
					new SQLiteParameter("@pcount",orderList.Pcount)
				};
				int newId = DBHelper.ExecuteCommand(sql, para);
				return newId;
			 
		}
		#endregion
		#region 删除数据以主键，标识列，唯一列构建的删除
			
		public static int DeleteOrderListById(long id)	
		{
			string sql="DELETE From  OrderList  Where id=@id";
			 SQLiteParameter[] para = new SQLiteParameter[]
				{
					new SQLiteParameter("@id", id)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}		
		
		#endregion
		#region 修改数据
		//考虑标识列与主键不一致情形
		//以标识列修改实体
		public static int ModifyOrderListByid(OrderList orderList)
		{
		string sql ="UPDATE "+"OrderList"+" set "+
					"CustomerID=@CustomerID, "+
					"CType=@CType, "+
					"OrderDate=@OrderDate, "+
					"OrderName=@OrderName, "+
					"pcount=@pcount "+ 
				" Where id=@id";
			 
				SQLiteParameter[] para = new SQLiteParameter[]
				{
					new SQLiteParameter("@id",orderList.Id),
					new SQLiteParameter("@CustomerID",orderList.CustomerID),
					new SQLiteParameter("@CType",orderList.CType),
					new SQLiteParameter("@OrderDate",orderList.OrderDate),
					new SQLiteParameter("@OrderName",orderList.OrderName),
					new SQLiteParameter("@pcount",orderList.Pcount)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}					
		#endregion
		#region 查询数据
		public static IList<OrderList> GetAllOrderLists()
		{
			string sqlAll = "SELECT * FROM OrderList";
			return GetOrderListsBySql(sqlAll);
		}
		//以标识列获得对象
		public static OrderList GetOrderListByid(object id)
		{
			string sql = "SELECT * FROM "+"OrderList"+" WHERE id = @id";
			
            using (SQLiteDataReader reader = DBHelper.GetReader(sql, new SQLiteParameter("@id", id)))
			{
				 
				if (reader.Read())
				{
					OrderList orderList = new OrderList();
					orderList.Id=Convert.ToInt32(reader["id"]);
					orderList.CustomerID=Convert.ToString(reader["CustomerID"]);
					orderList.CType=Convert.ToString(reader["CType"]);
					orderList.OrderDate=Convert.ToDateTime(reader["OrderDate"]);
					orderList.OrderName=Convert.ToString(reader["OrderName"]);
					orderList.Pcount=Convert.ToString(reader["pcount"]);
					reader.Close();
					
					return orderList;
					}
				else
				{
					reader.Close();
					return null;
				}
			}
			 
		}
					
		//私有方法供构建好的SQL语句调用
		private static IList<OrderList> GetOrderListsBySql(string safeSql)
		{
			List<OrderList> list = new List<OrderList>();
			 
            using (DataTable table = DBHelper.GetDataTable(safeSql))
			{
				foreach (DataRow row in table.Rows)
				{
					OrderList orderList = new OrderList();
					orderList.Id=Convert.ToInt32(row["id"]);
					orderList.CustomerID=Convert.ToString(row["CustomerID"]);
					orderList.CType=Convert.ToString(row["CType"]);
					orderList.OrderDate=Convert.ToDateTime(row["OrderDate"]);
					orderList.OrderName=Convert.ToString(row["OrderName"]);
					orderList.Pcount=Convert.ToString(row["pcount"]);
					list.Add(orderList);
				}
				return list;
			}
             
		}
		private static IList<OrderList> GetOrderListsBySql( string sql, params SQLiteParameter[] values )
		{
			List<OrderList> list = new List<OrderList>();
			 
            using (DataTable table = DBHelper.GetDataTable(sql, values))
			{
				foreach (DataRow row in table.Rows)
				{
					OrderList orderList = new OrderList();
					orderList.Id=Convert.ToInt32(row["id"]);
					orderList.CustomerID=Convert.ToString(row["CustomerID"]);
					orderList.CType=Convert.ToString(row["CType"]);
					orderList.OrderDate=Convert.ToDateTime(row["OrderDate"]);
					orderList.OrderName=Convert.ToString(row["OrderName"]);
					orderList.Pcount=Convert.ToString(row["pcount"]);
					list.Add(orderList);
				}
				return list;
			}
            
			 
		}
		#endregion
		 
	}
}

